var papersSize = {
    A4: '50,28',
    A3: '297,420',
    B5: '176,250'
}
var printObj;
$(function () {
    // printObj = window.opener.window.printObj;
    // if (printObj == null) {
    //     alert("无法获取数据，请联系代理通管理员！");
    //     return;
    // }
    //开始设置页面格式
    printObj={
            paper:'A4',
            xy:'x',
            paddingTop: 0,
            paddingRight:0,
            paddingBottom:0,
            paddingLeft:0,
            fontSize:10,
            title:'数据未获取成功',
            pageHead:'<table><tr><td>数据未获取成功</td></tr></table>',
            pageFoot:'<table><tr><td>数据未获取成功</td></tr></table>',
            tableHead:'<tr><td>数据未获取成功</td></tr>',
            data:null,
            pageSize:9
        }
    _default();

});

function _default() {
    $("#paper").val(printObj.paper);
    $("#top").val(printObj.paddingTop);
    $("#left").val(printObj.paddingLeft);
    $("#right").val(printObj.paddingRight);
    $("#bottom").val(printObj.paddingBottom);
    $("#pageSize").val(printObj.pageSize);
    $("#fontSize").val(printObj.fontSize);
    printObj.xy == 'x' ? $("#x").attr("checked", true) : $("#y").attr("checked", true);
    format();
}

function format() {
    // empty();
    // setData();
    var xy = eval('papersSize.' + $("#paper").val());
    $(".pages").css('width', ($('input[id="x"]:checked').val() == 'x' ? xy.split(',')[0] : xy.split(',')[1]) - $("#left").val() - $("#right").val() + 'mm');
    $(".pages").css('height', ($('input[id="y"]:checked').val() == 'y' ? xy.split(',')[0] : xy.split(',')[1]) - $("#top").val() - $("#bottom").val() - 2 + 'mm');
    $(".pages").css('padding', $("#top").val() + 'mm ' + $("#right").val() + 'mm ' + $("#bottom").val() + 'mm ' + $("#left").val() + 'mm');
    $(".pages").css('font-size', $("#fontSize").val() + 'pt');
    $(".tabborder").css('table-layout', printObj.tableLayout);
    $("#tab_total").css('height', $(".pages").css('height').substring(0, $(".pages").css('height').length - 2) - 25 + 'mm');
}

function empty() {
    $(".printPaper").html('');
}

function setData() {
    var dataSourse = printObj.data;
    var columns = printObj.columns;
    var data = new Array();
    for (var i = 0; i < dataSourse.length; i++) {
        var contentArray = new Array();
        for (var j = 0; j < columns.length; j++) {
            if (columns[j]._hide) continue;
            var html = " ";
            var render = printObj.columns[j].render;
            if (render != null) {
                html = render(dataSourse[i]);
            } else
                html = dataSourse[i][columns[j].columnname] ? dataSourse[i][columns[j].columnname] : " "
            contentArray.push(html);//根据前台的列名进行排序，可以继续改进，加上隐藏与否的校验。目前没有
        }
        data[i] = contentArray;
    }
    var maxPage = 0;
    for (var i = 0; i < data.length; i++) {
        var page = parseInt(i / $("#pageSize").val());
        maxPage = page;
        var nextPage = page * 1 + 1;
        if (i % $("#pageSize").val() == 0) {
            var pageDivClass = 'pages PageNext';
            if (i >= data.length && printObj.totalPage == null) {
                pageDivClass = 'pages';
            }
            $('<div class="' + pageDivClass + '" id="pages_' + page + '">' +
                '<div class="title" ></div>' +
                printObj.pageHead +
                '<table id="pages_tb_' + page + '" border="0" cellpadding="0" cellspacing="0" class="tabborder"></table>' +
                '<div class="foot" id="pages_foot_' + page + '">' +
                printObj.pageFoot + '</div></div>').appendTo($('.printPaper'));
            $(printObj.tableHead).appendTo($("#pages_tb_" + page));
            $("#pages_foot_" + page).find('#page').html(nextPage);

        }
        nowTable = document.getElementById("pages_tb_" + page);
        addRow(nowTable, data[i]);
    }

    if (printObj.totalRow != null) {
        $('<tr>' + printObj.totalRow + '</tr>').appendTo($('#pages_tb_' + maxPage));
    }
    if (printObj.totalPage != null) {
        $('<div class="pages" id="pages_total"><div class="title" ></div>' +
            printObj.pageHead +
            '<table class="tabborder td_c" id="tab_total"  border="0" cellpadding="0" cellspacing="0" ><tr><td height="38" align="center" valign="top">' +
            printObj.totalPage.replace(';"', ';" class="td_unborder_r" align="center" border=0 cellpadding=0 cellspacing=0 ')
            + '<br/> <div style="text-align: left; padding:15px;">备注栏：</div> </td></tr></table>'
        ).appendTo($('.printPaper'));
    }
    $('.maxPage').html(maxPage + 1);
    $(".title").html(printObj.title);
    $('.nowDate').html(nowDate("yyyy年MM月dd日", 0, 0, 0));
}

function addRow(table, contentArray) {
    var tableRow = table.insertRow(table.rows.length);
    tableRow.id = "tableRow_" + (table.rows.length - 1);
    for (var i = 0; i < contentArray.length; i++) {
        var cell = tableRow.insertCell(i);
        cell.innerHTML = contentArray[i];
    }
}

function insetRow(table, rowIndex, contentArray) {
    var tableRow = table.insertRow(rowIndex);
    tableRow.id = "tableRow_" + rowIndex;
    for (var i = 0; i < contentArray.length; i++) {
        var cell = tableRow.insertCell(i);
        cell.innerHTML = contentArray[i];
    }
}

function nowDate(formate, addYear, addMonth, addDate, showTime) {
    var now = new Date();
    var year = now.getFullYear() + addYear;
    var month = now.getMonth() + 1 + addMonth;
    month = "0" + month;
    month = month.substring(month.length - 2);
    var day = now.getDate() + addDate;
    day = "0" + day;
    day = day.substring(day.length - 2);
    if (showTime) {
        var hour = "0" + now.getHours();
        hour = hour.substring(hour.length - 2, hour.length);
        var minute = "0" + now.getMinutes();
        minute = minute.substring(minute.length - 2, minute.length);
        day += (" " + hour + ":" + minute);
    }
    if (formate == '-')
        return year + "-" + month + "-" + day;

    else
        return year + "年" + month + "月" + day + "日";

};

function printIE() {
    //$(".pages").css('height','auto');多减了2mm以防空白页问题
    window.print();
    //$(".pages").css('height',($('input[id="y"]:checked').val()=='y'?xy.split(',')[0]:xy.split(',')[1])-$("#top").val()-$("#bottom").val()+'mm');
}